Ext.onReady(function(){
    Ext.QuickTips.init();
    clientlist = new ClientList({
        tbar: new Ext.Toolbar({
			height: 60,
            items: [{
                text: 'Edit',
				iconCls: 'edit',
				iconAlign: 'top',
				width: 40,
				scale: 'large',
                handler: function(btn){
                    var sel = clientlist.getSelectionModel().getSelected();
                    if (!sel) {
                        Ext.Msg.alert('Alert', 'No client selected!')
                        return
                    }
                    if (sel) {
                        var clientform = new ClientForm({
                            buttons: [{
                                text: 'Save',
                                handler: function(btn, e){
                                    btn.findParentByType('window').findByType('form')[0].getForm().submit({
                                        clientValidation: true,
                                        url: '/client',
                                        method: 'post',
                                        params: {
                                            action: 'update'
                                        },
                                        
                                        success: function(form, action){
                                            Ext.Msg.alert('Success', action.result.msg);
                                            clientlist.getStore().load();
                                            btn.findParentByType('window').destroy()
                                        },
                                        failure: function(form, action){
                                            if (action.failureType == 'client') {
                                                Ext.Msg.alert('Invalid Entry', 'Please fix the errors on your form and try again.')
                                            }
                                            else {
                                                Ext.Msg.alert('Server error', action.result.msg)
                                            }
                                        }
                                    });
                                }
                            }, {
                                text: 'Cancel',
                                handler: function(btn){
                                    btn.findParentByType('window').destroy()
                                }
                            }]
                        });
                        var win = new Ext.Window({
                            title: sel.data.client_name,
                            autoWidth: true,
                            height: 310,
                            items: [clientform],
                            layout: 'fit',
                            modal: true
                        });
                        win.show();
                        clientform.getForm().loadRecord(sel)
                    }
                    
                    
                }
            },'-', {
                text: 'New',
				iconCls: 'add',
				
				iconAlign: 'top',
				width: 40,
				scale: 'large',
                handler: function(btn){
                
                
                    var clientform = new ClientForm({
                        buttons: [{
                            text: 'Save',
                            handler: function(btn, e){
                                btn.findParentByType('window').findByType('form')[0].getForm().submit({
                                    clientValidation: true,
                                    url: '/client',
                                    method: 'post',
                                    params: {
                                        action: 'new'
                                    },
                                    
                                    success: function(form, action){
                                        Ext.Msg.alert('Success', action.result.msg);
                                        clientlist.getStore().load();
                                        btn.findParentByType('window').destroy()
                                    },
                                    failure: function(form, action){
                                        if (action.failureType == 'client') {
                                            Ext.Msg.alert('Invalid Entry', 'Please fix the errors on your form and try again.')
                                        }
                                        else {
                                            Ext.Msg.alert('Server error', action.result.msg)
                                        }
                                    }
                                });
                            }
                        }, {
                            text: 'Cancel',
                            handler: function(btn){
                                btn.findParentByType('window').destroy()
                            }
                        }]
                    });
                    var win = new Ext.Window({
                        title: 'New Client',
                        autoWidth: true,
                        height: 310,
                        items: [clientform],
                        layout: 'fit',
                        modal: true
                    });
                    win.show();
                    
                    
                    
                    
                }
            }, '-', {
                text: 'Delete',
				iconAlign: 'top',
				width: 40,
				scale: 'large',
				iconCls: 'remove',
                handler: function(btn, e){
                    Ext.MessageBox.confirm('Confirm', 'Are you sure you want to delete the client?', function(btn, text){
                        if (btn == 'yes') {
                            var sel = clientlist.getSelectionModel().getSelected();
                            if (!sel) {
                                Ext.Msg.alert('Alert', 'No client selected!')
                                return
                            }
                            Ext.Ajax.request({
                                url: '/client',
                                params: {
                                    client_id: sel.get('client_id'),
                                    action: 'delete'
                                },
                                method: 'post',
                                success: function(result, request){
                                    var jsondata = Ext.util.JSON.decode(result.responseText);
                                    if (jsondata.success) {
                                        Ext.Msg.alert('Success', jsondata.msg);
                                        clientlist.getStore().load();
                                    }
                                    else {
                                        Ext.Msg.alert('Failed to delete the record')
                                    }
                                }
                            });
                        }
                    })
                    
                }
            }]
        
        })
    });
    
    
    clientlist.store.load();
    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [new Ext.BoxComponent({ // raw element
            region: 'north',
            el: 'header',
            height: 20,
            html: '<div style="text-align:center;color: #15428B;font:16px;font-weight:bold;margin: 5px;">DLZ CONCRETE DATABASE v0.1</div>'
        }), clientlist, {
            region: 'center'
        }]
    });
    
});

